package com.snif.web.attach.core.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 类型转换工具类
 */
public class ClassTik {

	/**
	 * 根据类型名称获取对应的Class对象
	 */
	public static Class<?> getTypeClass(String typeName) {
		if (typeName == null || typeName.trim().isEmpty()) {
			return String.class;
		}

		switch (typeName.toLowerCase()) {
		case "string":
			return String.class;
		case "int":
		case "integer":
			return Integer.class;
		case "long":
			return Long.class;
		case "double":
			return Double.class;
		case "float":
			return Float.class;
		case "boolean":
			return Boolean.class;
		case "byte":
			return Byte.class;
		case "short":
			return Short.class;
		case "char":
		case "character":
			return Character.class;
		default:
			// 尝试加载自定义类
			try {
				return Class.forName(typeName);
			}
			catch (ClassNotFoundException e) {
				System.out.println("警告: 无法加载类型: " + typeName + ", 使用String类型");
				return String.class;
			}
		}
	}
	/**
	 * 解析Agent参数为键值对 支持格式：-javaagent:probe.jar=port=8088,debug=true
	 */
	public static Map<String, String> parseAgentArgs(String agentArgs) {
		Map<String, String> params = new HashMap<>();
		if (agentArgs == null || agentArgs.trim().isEmpty()) {
			return params;
		}

		// 支持逗号或分号分隔参数
		String[] pairs = agentArgs.split("[,;]");
		for (String pair : pairs) {
			pair = pair.trim();
			if (pair.contains("=")) {
				String[] keyValue = pair.split("=", 2);
				if (keyValue.length == 2) {
					params.put(keyValue[0].trim(), keyValue[1].trim());
				}
			}
		}
		return params;
	}

}
